﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class Frmmmxy : Form
    {
        public Frmmmxy()
        {
            InitializeComponent();
        }
        public DataSet ds = null;
        
        public Frmmmxy(string userid)
        {
            //InitializeComponent();
            UserInfoData data = new UserInfoData();
            data.Userid = userid;//获取用户名
            DataSet ds = UserInfoOperation.getUserInfoAll(data);
            if(ds.Tables[0].Rows.Count>0)//判断是否有该用户
            {
                this.txtUserid.Text = ds.Tables[0].Rows[0]["用户名"].ToString();
            }
        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtOpwd.Text))
                {
                    //密码为空
                    MessageBox.Show("密码不为空！");
                    return;
                }
                int result
                    = DataAccess.ReadData(this.txtUserid.Text.Trim(), this.txtOpwd.Text.Trim());
                if (result <= 0)
                {
                    MessageBox.Show("原密码不正确！", "提示");
                    this.txtOpwd.Focus(); return;
                }
                //新密码为空，这就不判断了
                if (this.txtONpwd.Text.Trim() != this.txtNpwd.Text.Trim())
                {
                    //两次输入密码不一致
                    MessageBox.Show("确认密码不正确！", "提示");
                    this.txtNpwd.Focus();
                }
               

                if (DataAccess.UpdateData(this.txtUserid.Text.Trim(), txtONpwd.Text.Trim()) > 0)
                {
                    MessageBox.Show("修改成功！", "提示");
                    this.Dispose();
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show("修改失败！", "错误");
            }
        }

        private void Frmmmxy_Load(object sender, EventArgs e)
        {
            //默认显示用户名
            this.txtUserid.Text = Login.name;
        }

        private void btnCannel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
    //UserInfoData类：
    public class UserInfoData
    {
        private string userid = "";
        private string userpwd = "";
        private string userlevel = "";
        //登录用户名
        public string Userid
        {
            get { return userid; }
            set { userid = value; }
        }
        //登录用户密码
        public string Userpwwd
        {
            get { return userpwd; }
            set { userpwd = value; }
        }
        //登录用户身份
        public string Userlevel
        {
            get { return userlevel; }
            set { userlevel = value; }

        }
    }
    //UserInfoOperation类
    public class UserInfoOperation
    {
        private static DataAccess dataAccess = new DataAccess();
        public static bool insertUserInfo(UserInfoData data)
        {
            //数据插入
            string sql = "insert into 用户表(用户名,用户密码,用户权限) values(" +
                "'" + data.Userid + "'," + "'" + data.Userpwwd + "'," +
                "'" + data.Userlevel + "')";
            return dataAccess.ExecuteSQL(sql);
        }
        public static bool updateUserInfo(UserInfoData Data)
        {//数据修改
            string sql = "Update 用户表 set 用户密码='" + Data.Userpwwd
            + "'/," + "用户权限='" + Data.Userlevel + "'" +
            "where 用户名='" + Data.Userid + "'";
            return dataAccess.ExecuteSQL(sql);

        }
        public static bool deleteUserInfo(string userid)
        {//数据删除
            string sql = String.Format("delete 用户表 where 用户名='{0}'", userid);
            return dataAccess.ExecuteSQL(sql);
        }
        public static DataSet getUserInfo(UserInfoData data)
        {//数据查询
            string condition = "";
            if (data.Userid != null && data.Userid != "")
            {
                condition += " and 用户名='" + data.Userid + "'";
            }
            if (data.Userlevel != null && data.Userlevel != "")
            {
                condition += "and 用户权限='" + data.Userlevel + "'";
            }
            string sql = "select 用户名,用户权限 from 用户表 where 1=1" +
                condition;
            return dataAccess.GetDataSet(sql, "用户表");
        }
        //getUserInfoAll
        public static DataSet getUserInfoAll(UserInfoData data)
        {//数据查询
            string condition = "";//查询的条件
            if (data.Userid != null && data.Userid != "")
            {
                condition += " and 用户名='" + data.Userid + "'";
            }
            if (data.Userlevel != null && data.Userlevel != "")
            {
                condition += " and 用户权限='" + data.Userlevel + "'";
            }
            string sql = "select 用户名,用户密码,用户权限 from 用户表 where 1=1" + condition;
            return dataAccess.GetDataSet(sql, "用户表");
        }
    }
}